package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.dto.PurchaseDTO;
import com.example.domain.Purchase;
import com.example.service.PurchaseService;
import com.example.mapper.PurchaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 惠普
 * @description 针对表【purchase】的数据库操作Service实现
 * @createDate 2022-09-27 15:05:30
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase>
		implements PurchaseService {

	/**
	 * 条件查询订单
	 */
	@Override
	public Page<Purchase> getPurchaseByCondition(PurchaseDTO dto) {
		Page<Purchase> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
		QueryWrapper<Purchase> query = new QueryWrapper<>();
		query.eq(ObjectUtils.isNotNull(dto.getUserId()), "user_id", dto.getUserId())
				.eq(ObjectUtils.isNotNull(dto.getMerchantId()), "merchant_id", dto.getMerchantId())
				.eq(ObjectUtils.isNotNull(dto.getIsShopping()), "is_shopping", dto.getIsShopping())
				.and(ObjectUtils.isNotNull(dto.getRiderId()), qr -> qr
						.eq(ObjectUtils.isNotNull(dto.getRiderId()), "rider_id", dto.getRiderId())
						.or()
						.eq(ObjectUtils.isNotNull(dto.getOrderState()), "order_state", dto.getOrderState())
				);

		return baseMapper.selectPage(page, query);
	}
}




